package efr.dao.impl;

import java.util.List;

import org.hibernate.Session;

import efr.dao.AutorDao;
import efr.entity.Autor;
import efr.util.HibernateUtil;

public class AutorDaoImpl implements AutorDao{

	@SuppressWarnings("unchecked")
	@Override
	public List<Autor> getAllAutori() {
		List<Autor> listAutori = null;
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		listAutori = session.createQuery("FROM Autor").list();
		session.getTransaction().commit();
		return listAutori;
	}

	@Override
	public Autor getAutor(int id) {
		Autor autor = null;
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		autor = (Autor)session.get(Autor.class, id);
		session.getTransaction().commit();
		return autor;
	}

	@Override
	public void updateAutor(Autor autor) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		session.update(autor);
		session.getTransaction().commit();
	}

	@Override
	public void deleteAutor(Autor autor) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		session.delete(autor);
		session.getTransaction().commit();
	}

	@Override
	public Integer addAutor(Autor autor) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		Integer id = (Integer)session.save(autor);
		session.getTransaction().commit();
		return id;
	}

}
